﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Ex13.ExtractXLMtags
{
    class ExtractXLMtags
    {
        static void Main(string[] args)
        {
            //string text = "<name><first>Ivo</first></name><age>29</age>";
            //string searchPattern = @"<(?<tagName>[^(>|/)]+)>(?<content>(.|\s)*?)</${tagName}>";

            //Match match = Regex.Match(text,searchPattern);
            //while (match.Success)
            //{
            //    Group group = match.Groups["tagName"];
            //    Console.WriteLine("tag={0}",group.Value);
            //    group = match.Groups["content"];
            //    Console.WriteLine("content={0}", group.Value);

            //    match = match.NextMatch();
            //}

            string text = "<name><first>Ivo</first></name><age>29</age>";
            string tagPattern = @"\<(?<tag>\w+)\>";
            MatchCollection tags = Regex.Matches(text, tagPattern);
            foreach (Match tag in tags)
            {
                Match tagName = Regex.Match(tag.Value, @"\w+");
                string tagStartPattern = @"<" + Regex.Escape(tagName.Value) + @">";
                string tagEndPattern = @"</" + Regex.Escape(tagName.Value) + @">";

                string tagFinalPattern = Regex.Escape(tagStartPattern) + @"(?<value>(.|\s)+)" + Regex.Escape(tagEndPattern);
                Match tagValue = Regex.Match(text, tagFinalPattern);
                Group gr = tagValue.Groups["value"];
                
                Console.WriteLine("tag={0}, value={1}", tagName, gr.Value);
            }  
        }
    }
}
